﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace JiuMeiMall.PMethod
{
    /// <summary>
    /// Web请求操作
    /// </summary>
    /// <remarks>
    /// 作者：平杨.PY
    /// </remarks>
    public class UrlResponse
    {
        #region POST public static string GetResponseString(string apiUrl, string postData, int timeOut = 20000)
        /// <summary>
        /// POST
        /// </summary>
        /// <param name="apiUrl">请求地址</param>
        /// <param name="postData">参数字符串</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>string</returns>
        public static string GetResponseString(string apiUrl, string postData, int timeOut = 20000)
        {
            string errorMessage;
            return GetResponseString(apiUrl, postData, Encoding.UTF8, out errorMessage, null, timeOut);
        }
        #endregion

        #region POST public static string GetResponseString(string apiUrl, string postData, Encoding encoding, out string errorMessage, int timeOut = 20000)
        /// <summary>
        /// POST
        /// </summary>
        /// <param name="apiUrl">请求地址</param>
        /// <param name="postData">参数字符串</param>
        /// <param name="encoding">接收编码</param>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>string</returns>
        public static string GetResponseString(string apiUrl, string postData, Encoding encoding, out string errorMessage, int timeOut = 20000)
        {
            return GetResponseString(apiUrl, postData, encoding, out errorMessage, null, timeOut);
        }
        #endregion

        #region POST public static string GetResponseString(string apiUrl, string postData, Encoding encoding, out string errorMessage, CookieCollection cookieCollection, int timeOut = 20000)
        /// <summary>
        /// POST
        /// </summary>
        /// <param name="apiUrl">请求地址</param>
        /// <param name="postData">参数字符串</param>
        /// <param name="encoding">接收编码</param>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="cookieCollection">带cookie信息</param>
        /// <param name="timeOut">超时时间</param>
        /// <returns>string</returns>
        public static string GetResponseString(string apiUrl, string postData, Encoding encoding, out string errorMessage, CookieCollection cookieCollection, int timeOut = 20000)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
            httpWebRequest.Method = "POST";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded;";
            httpWebRequest.ContentLength = postData.Length;
            httpWebRequest.Timeout = timeOut;
            //创建Cooike并发送Cooike
            if (cookieCollection != null)
            {
                var cookieContainer = new CookieContainer();
                cookieContainer.Add(cookieCollection);
                httpWebRequest.CookieContainer = cookieContainer;
            }

            errorMessage = string.Empty;
            HttpWebResponse response = null;

            try
            {
                var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream());
                streamWriter.Write(postData);
                streamWriter.Close();

                response = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            finally
            {
                if (response != null)
                    response.Close();
            }

            return string.Empty;
        }
        #endregion

        #region GET public static string GetResponseString(string apiUrl)
        /// <summary>
        /// GET
        /// </summary>
        /// <param name="apiUrl">请求地址</param>
        /// <returns></returns>
        public static string GetResponseString(string apiUrl)
        {
            string errorMessage;
            return GetResponseString(apiUrl, Encoding.UTF8, out errorMessage);
        }
        #endregion

        #region GET public static string GetResponseString(string apiUrl, Encoding encoding, out string errorMessage, int timeOut = 20000)
        /// <summary>
        /// GET
        /// </summary>
        /// <param name="apiUrl">URL</param>
        /// <param name="encoding">接收编码</param>
        /// <param name="errorMessage">错误信息</param>
        /// <param name="timeOut">超时时间（默认值20s）</param>
        /// <returns></returns>
        public static string GetResponseString(string apiUrl, Encoding encoding, out string errorMessage, int timeOut = 20000)
        {
            errorMessage = string.Empty; 
            try
            {
                var request = WebRequest.Create(apiUrl);
                request.Method = "GET";
                request.Timeout = timeOut;
                var jsonStream = new StreamReader(request.GetResponse().GetResponseStream(), encoding);

                return jsonStream.ReadToEnd();
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            return string.Empty;
        }
        #endregion

    }
}
